package com.mmall.concurrency.example.singleton;

import com.mmall.concurrency.annoation.Recommend;
import com.mmall.concurrency.annoation.ThreadSafe;

/**
 * 枚举模式：最安全
 */
@ThreadSafe
@Recommend
public class SingletonExample7 {

    //私有构造函数
    private SingletonExample7(){

    }

    public SingletonExample7 getInstance(){

        return Singleton.INSTANCER.getInstance();
    }

    private enum Singleton{

        INSTANCER;

        private SingletonExample7 singleton;

        //JVM保证构造函数只被调用一次
        Singleton(){
            singleton = new SingletonExample7();
        }

        public SingletonExample7 getInstance(){
            return singleton;
        }
    }
}
